eststo clear

if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\DiD\"
	local plotSaveDir = "Plots\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/DiD/"
	local plotSaveDir = "Plots/"
	local pythonSaveDir = "PythonScripts/"
}

//With linear and quadratic time trends
//Establish Control Variable Sets
local spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"
local spec2Controls = "`spec1Controls' i.transactionDataDummy"
local spec3Controls = "`spec2Controls' i.regDummyExtSpec3"
local spec4Controls = "`spec2Controls' i.regDummyExtSpec4"

//Establish ci level
local ciLevel = 95

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Estimate Models
//Estimate Primary Spec. 2 DiD Models
regFWL logregistrations, nosafetyind, `spec2Controls', `absorbVars', `clusterVar', _
matlist e(b)
matlist e(V)
//Estimate Alternative Spec. 2 DiD Model 
regFWL logregistrations, nosafetyind metroIntTreatment, `spec2Controls', `absorbVars', `clusterVar', _
matlist e(b)
matlist e(V)
eststo spec2
local df = e(N_clust)-1
addRobustFStat "mean:nosafetyind_res"
addTreat "mean:nosafetyind_res"

test [mean]nosafetyind_res
test [mean]metroIntTreatment_res

estadd local dsc = "Documented" 
estadd local groupFE = "Y"
estadd local timeFE = "Y"
estadd local timeTrend = "Y"
estadd local quadTrend = "Y"
estadd local commonControls = "Y"
estadd local modelNum = "(2)"


//Export Model Results to TeX Files
//Establish Format
local fmtDiD = `"drop(*) nocon label compress starlevels(* 0.1 ** 0.05 *** 0.01) nodepvar nomtitle nonumbers not nostar noline"'


// local noteString = "\hline \hline \end{tabular} \vspace{0.2cm} \begin{tablenotes} \begin{spacing}{0} \begin{footnotesize} \item \textbf{Notes:} This table presents difference-in-differences estimates of the impact of the removal of safety inspections on the log of vehicle registrations.  The unit of observation is state-year. The four specifications pertaining to different levels of ``Data Source Controls'' (DSCs) and control specifications are documented in Table \cref{ssec:Spec}. Standard errors, reported in parentheses, are clustered by state. * p\textless 0.1, ** p\textless 0.05, *** p\textless 0.01 \end{footnotesize} \end{spacing} \end{tablenotes} \end{table}"
//
// //Export Table
// esttab spec1 spec2 spec3 spec4 using "`tableSaveDir'RegDiD.tex", `fmtDiD' title("Diff-in-Diff Results \label{tab:RegDiD}") stats(modelNum treatb treatse dsc groupFE timeFE timeTrend quadTrend commonControls fStat N r2, labels("\hline \hline \multicolumn{5}{c}{Dep. Var.: Log Number of Vehicle Registrations} \\ " "Removal of" "Safety Inspections" "\makecell[l]{Data Source \nextline Controls}" "State Fixed Effects" "Year Fixed Effects" "\makecell[l]{State-Specific Linear \\ Time Trends}" "\makecell[l]{State-Specific Quadratic \\ Time Trends}" "Common Controls" "Robust F-Stat" "Number of Obs." "R\textsuperscript{2}")fmt(%8.3f %8.3f %8.3f %8.3f  %8.3f  %8.3f  %8.3f  %8.3f %8.3f %8.0fc %8.3f)) replace postfoot("`noteString'")
